import { vnode } from "./vnode";
/**
 *
 * @param {string} sel
 * @param {属性} data
 * @param {子节点,文本内容或子dom节点} child
 * @one h('div',{},'文本')
 * @two h('div',{},[])
 * @three h('div',{},h())
 */
export function h(sel, data, child) {
  //传参个数必须为3
  if (arguments.length !== 3) {
    throw new SyntaxError("参数个数错误");
  }
  //检查第三个参数内容
  if (typeof child === "string" || typeof child === "number") {
    //文本
    return vnode(sel, data, undefined, child, undefined);
  } else if (Array.isArray(child)) {
    //数组
    for (let i = 0; i < child.length; i++) {
      if (!(typeof child[i] === "object" && child[i].hasOwnProperty("sel"))) {
        throw new SyntaxError("传入数组参数不对");
      }
    }
    return vnode(sel, data, child, undefined, undefined);
    //h函数返回结果
  } else if (typeof child === "object" && child.hasOwnProperty("sel")) {
    let children = [child];
    return vnode(sel, data, children, undefined, undefined);
    //啥也不是
  } else {
    throw new TypeError("传入第三个参数类型错误");
  }
}
